package com.example.seh.chat;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.icu.lang.UScript;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.seh.R;
import com.example.seh.UserDTO;
import com.example.seh.util.ImgUtil;

import java.io.IOException;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChatAdapter extends BaseAdapter {
    private Context mContext;
    private List<ChatUserDTO> marketers;
    private int item;

    public ChatAdapter(Context mContext, List<ChatUserDTO> marketers, int item) {
        System.out.println("adapter运行");
        this.mContext = mContext;
        this.marketers = marketers;
        this.item = item;
    }

    @Override
    public int getCount() {
        if (marketers != null) {
            return marketers.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (marketers != null) {
            return marketers.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.avatar = convertView.findViewById(R.id.markerters_avatar);
            viewHolder.name = convertView.findViewById(R.id.markerters_name);
            viewHolder.histoty = convertView.findViewById(R.id.markerters_history);
            viewHolder.time = convertView.findViewById(R.id.markerters_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), marketers.get(position).getAvatar());
//        bitmap = scaleBitmap(bitmap, (float) 0.5);
//        bitmap = scaleBitmap(bitmap, (float) 0.5);
//        bitmap = toRoundBitmap(bitmap);
//        viewHolder.avatar.setImageBitmap(bitmap);

            if (!"".equals(marketers.get(position).getUserDTO().getPic())) {
                Glide.with(mContext).load(marketers.get(position).getUserDTO().getPic()).into(viewHolder.avatar);
//                viewHolder.avatar.setImageBitmap(ImgUtil.toRoundBitmap(ImgUtil.stringToBitmap(marketers.get(position).getUserDTO().getPic())));
            }
            viewHolder.name.setText(marketers.get(position).getUserDTO().getUsername());
            if (marketers.get(position).getLastMessage() != null) {
                viewHolder.histoty.setText(marketers.get(position).getLastMessage());
            } else {
                viewHolder.histoty.setText("[图片]");
            }
            Log.e("time",marketers.get(position).toString());
            viewHolder.time.setText(marketers.get(position).getLastSendTime().substring(5,16));


//        viewHolder.time.setText(marketers.get(position).get());
        return convertView;
    }
    class ViewHolder{
        private CircleImageView avatar;
        private TextView name, histoty, time;
    }
}
